package com.ruoyi.web.controller.resturant;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.domain.Student;
import com.ruoyi.resturant.service.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/student")
@Api(tags = "学生用户信息管理")
public class StudentController extends BaseController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private RedisCache redisCache;
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sno", value = "学号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "String", dataTypeClass = String.class)
    })
    /**
     * 查询学生列表
     */

    @ApiOperation("取得所有学生列表")
    @GetMapping("/list")
    public AjaxResult  getstudents() {
    List<Student> rs = studentService.getStudents();
    return AjaxResult.success(rs);
    }

    /**
     * 获取学生详细信息
     * @param id
     * @return
     */
    @ApiOperation(value = "获取学生详细信息", notes = "TODO：取得某个学生的详细信息")
    @ApiImplicitParam(name = "id", value = "学生ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)


    @GetMapping(value = "/{id}")
    public AjaxResult getRedisObj(@PathVariable("id") Long id){
        // 1 根据店铺ID从redis取得店铺详情的json字符串
        String str = redisCache.getCacheObject(String.valueOf(id));
        // 2 把店铺详情的json字符串转换为一个model对象ResShop
        Student student = JSON.parseObject(str, Student.class);
        if (student == null) {
            // 3 redis不存在这个店铺的场合，从数据库里查询
            student = studentService.selectStudentById(id);
            // 4 数据库里该店铺ID不存在的场合，抛出业务异常BizException
            if(student == null) {
                throw new BizException(404, "该学生id("+ id + ")不存在");
            }
            String strDB = JSON.toJSONString(student);
            redisCache.setCacheObject(String.valueOf(id), strDB,
                    1, TimeUnit.HOURS);
        }
        return AjaxResult.success(student);
    }

    @Log(title = "学生管理",businessType = BusinessType.INSERT)
    @ApiOperation("新增学生")
    @PostMapping
    public AjaxResult add(@RequestBody Student student) {
        return AjaxResult.success(studentService.insertStudent(student));
    }

    /**
     * 修改店铺
     */
    @Log(title = "学生管理",businessType = BusinessType.UPDATE)
    @ApiOperation("修改学生")
    @PutMapping
    public AjaxResult edit(@RequestBody Student student) {
        //1先根据店铺ID查询数据库
       Student studentDB=  studentService.selectStudentById(student.getId());
        //如果该店铺不存在，就抛出业务异常
        if (studentDB == null){
            throw  new BizException(404,"该店铺id"+student.getId()+"不存在");
        }
        return AjaxResult.success(studentService.updateStudent(student));
    }


    /**
     * 删除店铺
     */
    @Log(title = "学生管理",businessType = BusinessType.DELETE)

    @ApiOperation("删除学生")
    @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
       return AjaxResult.success(studentService.deleteStudentById(id));
    }

}
